package cn.zf233.xcloud.config.common;

import cn.zf233.xcloud.util.OSSUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;

/**
 * Created by zf233 on 2021/1/12
 * OSS 配置类
 */
@Configuration
@EnableAsync
public class OSSConfig {

    @Value("${alibaba.oss.endpoint}")
    private String endpoint;

    @Value("${alibaba.oss.accessKeyId}")
    private String accessKeyId;

    @Value("${alibaba.oss.accessKeySecret}")
    private String accessKeySecret;

    @Value("${alibaba.oss.bucketName}")
    private String bucketName;
    @Bean
    public OSSUtil ossUtil() {
        return new OSSUtil(endpoint, accessKeyId, accessKeySecret, bucketName);
    }

    /*@Bean
    public OssClientFactory factory() {
        return new OssClientFactory(endpoint, accessKeyId, accessKeySecret);
    }

    public static class OssClientFactory {

        private volatile OSS client;
        private volatile OSSClientBuilder ossClientBuilder;

        private final String endpoint;
        private final String accessKeyId;
        private final String accessKeySecret;

        public OssClientFactory(String endpoint, String accessKeyId, String accessKeySecret) {
            this.endpoint = endpoint;
            this.accessKeyId = accessKeyId;
            this.accessKeySecret = accessKeySecret;
        }

        public OSS getOSSClient() {
            if (client == null) {
                synchronized (OssClientFactory.class) {
                    if (client == null) {
                        client = getOSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
                    }
                }
            }
            return client;
        }

        public OSSClientBuilder getOSSClientBuilder() {
            if (ossClientBuilder == null) {
                synchronized (OssClientFactory.class) {
                    if (ossClientBuilder == null) {
                        ossClientBuilder = new OSSClientBuilder();
                    }
                }
            }
            return ossClientBuilder;
        }
    }*/
}
